import os
import sys


def write_indent(f, indent_level: int):
    for _ in range(indent_level):
        f.write('    ')


def generate_structure(dirname: str, f, indent_level: int = 0):
    dir_entries = sorted(os.listdir(dirname), key=lambda x: (os.path.isfile(os.path.join(dirname, x)), x))
    for name in dir_entries:
        path = os.path.join(dirname, name)
        write_indent(f, indent_level)
        if os.path.isfile(path):
            f.write(name + '\n')
        else:
            f.write('/' + name + '\n')
            generate_structure(path, f, indent_level + 1)


def create_structure_file(directory: str, filename: str):
    with open(filename, 'w') as f:
        f.write('/' + os.path.basename(directory) + '\n')
        generate_structure(directory, f, 1)


if __name__ == '__main__':
    argv_len = len(sys.argv)
    if argv_len < 3:
        print('err usage', __file__, '<pathToFolder>', '<outputFileName>')
    else:
        create_structure_file(sys.argv[1], sys.argv[2])
